<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智慧Agent平台</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;700&display=swap');

        body {
            font-family: 'Noto Sans SC', sans-serif;
            background: linear-gradient(135deg, #f0f9ff 0%, #e6f4ff 100%);
        }

        .card-hover {
            transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
            border: 1px solid rgba(0, 0, 0, 0.05);
        }

        .card-hover:hover {
            transform: translateY(-8px);
            box-shadow: 0 15px 30px rgba(0, 105, 204, 0.1);
            border-color: rgba(59, 130, 246, 0.2);
        }

        .app-icon {
            width: 56px;
            height: 56px;
            border-radius: 16px;
            transition: all 0.3s ease;
        }

        .hero-text {
            background: linear-gradient(90deg, #2563eb 0%, #3b82f6 100%);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }

        .wave-bg {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            overflow: hidden;
            line-height: 0;
        }

        .wave-bg svg {
            position: relative;
            display: block;
            width: calc(100% + 1.3px);
            height: 150px;
        }

        .wave-bg .shape-fill {
            fill: #FFFFFF;
        }
    </style>
</head>
<body class="min-h-screen">
    <header class="bg-white/80 backdrop-blur-sm shadow-sm sticky top-0 z-50">
        <div class="container mx-auto px-4 py-3 flex justify-between items-center">
            <div class="flex items-center space-x-8">
                <div class="flex items-center space-x-3">
                    <div class="w-8 h-8 bg-blue-600 rounded-lg flex items-center justify-center">
                        <svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"></path>
                        </svg>
                    </div>
                    <h1 class="text-2xl font-bold text-blue-600">智慧Agent平台</h1>
                </div>
            </div>
            <div id="auth-buttons" class="flex items-center space-x-4">
                <!-- 动态内容由JavaScript填充 -->
            </div>
        </div>
    </header>

    <main class="container mx-auto px-4 py-8 relative">
        <section class="text-center mb-8 mt-4">
            <h2 class="text-3xl md:text-4xl font-bold hero-text mb-2 leading-tight">智慧Agent平台</h2>
            <p class="text-base text-gray-700 mb-4 max-w-2xl mx-auto">探索人工智能的无限可能，让智能服务助力您的工作</p>

            <div class="mt-6 max-w-4xl mx-auto">
                <lottie-player src="https://assets3.lottiefiles.com/packages/lf20_5tkzkblw.json"
                              background="transparent"
                              speed="1"
                              style="width: 100%; height: 200px;"
                              loop
                              autoplay></lottie-player>
            </div>
        </section>

        <section class="mb-16">
            <div class="flex justify-between items-center mb-8">
                <div>
                    <h3 class="text-2xl font-semibold text-gray-800">推荐应用</h3>
                    <p class="text-gray-500 mt-1">精选智能服务，提升工作效率</p>
                </div>
                <button onclick="fetchAgents()" class="text-blue-600 hover:text-blue-800 flex items-center group">
                    <span>换一换</span>
                    <svg class="w-5 h-5 ml-1 group-hover:translate-x-1 transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14 5l7 7m0 0l-7 7m7-7H3"></path>
                    </svg>
                </button>
            </div>

            <div id="agents-container" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
                <!-- 应用卡片将由JavaScript动态生成 -->
                <div class="text-center py-8">
                    <svg class="animate-spin -ml-1 mr-3 h-8 w-8 text-blue-600 inline-block" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    <p class="mt-2 text-gray-600">正在加载应用...</p>
                </div>
            </div>
        </section>
    </main>

    <footer class="bg-white py-8">
        <div class="container mx-auto px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <div class="flex items-center space-x-3">
                        <div class="w-8 h-8 bg-blue-600 rounded-lg flex items-center justify-center">
                            <svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"></path>
                            </svg>
                        </div>
                        <span class="text-xl font-bold text-blue-600">智慧Agent平台</span>
                    </div>
                    <p class="text-gray-500 mt-2 text-sm">让智能服务改变生活</p>
                </div>

                <div class="flex flex-col space-y-2 text-sm text-gray-600">
                    <div class="flex items-center">
                        <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path>
                        </svg>
                        <span>邮箱: forzenzhencola@163.com</span>
                    </div>
                    <div class="flex items-center">
                        <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"></path>
                        </svg>
                        <span>电话: 15191913497</span>
                    </div>
                    <div class="flex items-center">
                        <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path>
                        </svg>
                        <span>联系人: 王震</span>
                    </div>
                </div>
            </div>

            <div class="border-t border-gray-200 mt-8 pt-8 text-center text-gray-500 text-sm">
                <p>© 2025 王震. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <script>
        // 检查用户登录状态
        document.addEventListener('DOMContentLoaded', function() {
            checkAuthStatus();
            fetchAgents();
        });

        function checkAuthStatus() {
            const token = localStorage.getItem('access_token');
            const authButtons = document.getElementById('auth-buttons');

            if (token) {
                // 用户已登录
                try {
                    const userData = JSON.parse(localStorage.getItem('user_data'));
                    authButtons.innerHTML = `
                        <span class="text-gray-700">欢迎, ${userData.username}</span>
                        <a href="/admin" class="bg-gradient-to-r from-blue-600 to-blue-500 text-white px-6 py-2 rounded-lg hover:from-blue-700 hover:to-blue-600 transition-all shadow-md hover:shadow-lg">
                            管理后台
                        </a>
                        <button onclick="logout()" class="bg-gradient-to-r from-blue-600 to-blue-500 text-white px-6 py-2 rounded-lg hover:from-blue-700 hover:to-blue-600 transition-all shadow-md hover:shadow-lg">
                            退出登录
                        </button>
                    `;
                } catch (e) {
                    console.error('Error parsing user data:', e);
                    authButtons.innerHTML = `
                        <a href="/login" class="bg-gradient-to-r from-blue-600 to-blue-500 text-white px-6 py-2 rounded-lg hover:from-blue-700 hover:to-blue-600 transition-all shadow-md hover:shadow-lg">
                            点我登录
                        </a>
                    `;
                }
            } else {
                // 用户未登录
                authButtons.innerHTML = `
                    <a href="/login" class="bg-gradient-to-r from-blue-600 to-blue-500 text-white px-6 py-2 rounded-lg hover:from-blue-700 hover:to-blue-600 transition-all shadow-md hover:shadow-lg">
                        点我登录
                    </a>
                `;
            }
        }

        async function logout() {
            try {
                const response = await fetch('/api/logout', {
                    method: 'POST',
                    headers: {
                        'Authorization': `Bearer ${localStorage.getItem('access_token')}`,
                        'Content-Type': 'application/json'
                    }
                });

                if (response.ok) {
                    localStorage.removeItem('access_token');
                    localStorage.removeItem('user_data');
                    window.location.href = '/login';
                } else {
                    const data = await response.json();
                    alert(data.error || '退出登录失败');
                }
            } catch (error) {
                console.error('退出登录错误:', error);
                alert('网络错误，请稍后重试');
            }
        }

        // 获取Agent列表
        async function fetchAgents() {
            const container = document.getElementById('agents-container');
            container.innerHTML = `
                <div class="text-center py-8">
                    <svg class="animate-spin -ml-1 mr-3 h-8 w-8 text-blue-600 inline-block" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    <p class="mt-2 text-gray-600">正在加载应用...</p>
                </div>
            `;

            try {
                const token = localStorage.getItem('access_token');
                const headers = {
                    'Content-Type': 'application/json'
                };

                if (token) {
                    headers['Authorization'] = `Bearer ${token}`;
                }

                const response = await fetch('/api/agents', {
                    method: 'GET',
                    headers: headers
                });

                if (!response.ok) {
                    throw new Error('获取应用列表失败');
                }

                const agents = await response.json();
                renderAgents(agents);
            } catch (error) {
                console.error('获取应用列表错误:', error);
                container.innerHTML = `
                    <div class="text-center py-8">
                        <svg class="w-12 h-12 text-red-500 inline-block" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path>
                        </svg>
                        <p class="mt-2 text-gray-600">加载应用失败: ${error.message}</p>
                        <button onclick="fetchAgents()" class="mt-4 bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition-all">
                            重试
                        </button>
                    </div>
                `;
            }
        }

        // 渲染Agent列表
        function renderAgents(agents) {
            const container = document.getElementById('agents-container');

            if (!agents || agents.length === 0) {
                container.innerHTML = `
                    <div class="text-center py-8 col-span-full">
                        <svg class="w-12 h-12 text-gray-400 inline-block" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.172 16.172a4 4 0 015.656 0M9 10h.01M15 10h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                        </svg>
                        <p class="mt-2 text-gray-600">暂无可用应用</p>
                    </div>
                `;
                return;
            }

            container.innerHTML = agents.map(agent => `
                <div class="bg-white rounded-2xl shadow-md p-6 transition-all duration-300 card-hover flex flex-col">
                    <div class="flex items-start mb-4">
                        <div class="app-icon ${getRandomColorClass(agent.agent_id)} flex items-center justify-center mr-4 group-hover:bg-blue-200">
                            <svg class="w-7 h-7 ${getRandomTextColorClass(agent.agent_id)}" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"></path>
                            </svg>
                        </div>
                        <div>
                            <h4 class="text-lg font-semibold mb-1">${agent.name}</h4>
                            <p class="text-sm text-gray-500">${agent.llm_model_name}</p>
                        </div>
                    </div>
                    <p class="text-gray-600 text-sm mb-6">${agent.system_prompt || agent.greeting_message ||  'AI智能助手，智能对话，智能问答'}</p>
                    <div class="mt-auto flex space-x-3">
                        <a href="/chat?agent_id=${agent.agent_id}" class="flex-1 bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition-all text-center">
                            立即使用
                        </a>
                        <button onclick="showAgentDetails('${agent.agent_id}')" class="w-10 h-10 flex items-center justify-center bg-blue-50 text-blue-600 rounded-lg hover:bg-blue-100 transition-all">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                            </svg>
                        </button>
                    </div>
                </div>
            `).join('');
        }

        // 显示Agent详情
        function showAgentDetails(agentId) {
            // 这里可以添加显示Agent详情的逻辑
            alert(`查看Agent详情: ${agentId}`);
        }

        // 根据Agent ID生成随机颜色类
        function getRandomColorClass(agentId) {
            const colors = ['bg-blue-100', 'bg-purple-100', 'bg-green-100', 'bg-yellow-100', 'bg-pink-100', 'bg-indigo-100'];
            const index = parseInt(agentId.replace(/[^0-9]/g, '').slice(0, 1)) % colors.length;
            return colors[index];
        }

        // 根据Agent ID生成随机文本颜色类
        function getRandomTextColorClass(agentId) {
            const colors = ['text-blue-600', 'text-purple-600', 'text-green-600', 'text-yellow-600', 'text-pink-600', 'text-indigo-600'];
            const index = parseInt(agentId.replace(/[^0-9]/g, '').slice(0, 1)) % colors.length;
            return colors[index];
        }
    </script>
</body>
</html>